package com.yao2san.simcasclient.authentication.oauth;


import com.yao2san.simcasclient.config.ClientConfig;
import com.yao2san.simcasclient.config.ServerConfig;
import org.springframework.http.MediaType;
import com.yao2san.sim.framework.utils.RequestUtil;

import java.util.HashMap;
import java.util.Map;

public class ClientAuthorization implements Authorization {

    private ClientConfig clientConfig;
    private ServerConfig serverConfig;

    private String code;
    private String redirectUri;

    public ClientAuthorization(ClientConfig clientConfig, ServerConfig serverConfig) {
        this.clientConfig = clientConfig;
        this.serverConfig = serverConfig;
    }

    @Override
    public String getClientId() {
        return clientConfig.getId();
    }

    @Override
    public String getCode() {
        final String url = "http://" + serverConfig.getHost() + ":" + serverConfig.getPort() + Constants.AUTHORIZE_URI;
        Map<String, Object> params = new HashMap<>();
        params.put("client_id", clientConfig.getId());
        params.put("response_type", "code");
        params.put("redirect_uri", "");
        params.put("scope", "");
        params.put("state", "");
        String response = RequestUtil.postForObject(url, params, MediaType.APPLICATION_FORM_URLENCODED, String.class);
        return this.code;
    }

    @Override
    public String getRedirectUri() {
        return this.redirectUri;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public void setRedirectUri(String redirectUri) {
        this.redirectUri = redirectUri;
    }
}
